<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['basic:ipc:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['basic:ipc:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['basic:ipc:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['basic:ipc:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table border  v-loading="loading" :data="ipcList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="项目编号" align="center" prop="itemCode" />
      <el-table-column label="项目名称" align="left" prop="itemName" />
      <el-table-column label="正负标识" align="center" prop="itemSign">
        <template #default="scope">
          <el-tag type="primary" size="small" effect="dark" round v-if="scope.row.itemSign == 1">正数</el-tag>
          <el-tag type="danger" size="small" effect="dark" round v-else>负数</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="排序" align="center" prop="itemOrder" />
      <el-table-column label="状态" align="center" width="80">
        <template #default="scope">
          <el-switch
            v-model="scope.row.itemStatus"
            active-value="0"
            inactive-value="1"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="计算方式" align="center" prop="computeMode">
        <template #default="scope">
          <el-tag type="primary" v-if="scope.row.computeMode == 1">百章清单</el-tag>
          <el-tag type="primary" v-else-if="scope.row.computeMode == 2">求和</el-tag>
          <el-tag type="primary" v-else-if="scope.row.computeMode == 3">百分比</el-tag>
          <el-tag type="primary" v-else-if="scope.row.computeMode == 4">固定值</el-tag>
          <el-tag type="primary" v-else>其他</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="报表显示" align="center" prop="isShow">
        <template #default="scope">
          <el-switch
            v-model="scope.row.isShow"
            active-value="Y"
            inactive-value="N"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['basic:ipc:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['basic:ipc:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改基础中期支付证书对话框 -->
    <el-dialog :title="title" v-model="open" :close-on-click-modal="false" width="500px" append-to-body :close-on-click-modal="false">
      <el-form ref="ipcRef" :model="form" :rules="rules" label-width="90px">
        <el-form-item label="项目编号" prop="itemCode">
          <el-input v-model="form.itemCode" placeholder="请输入支付项目编号" maxlength="12" show-word-limit/>
        </el-form-item>
        <el-form-item label="项目名称" prop="itemName">
          <el-input v-model="form.itemName" placeholder="请输入支付项目名称" maxlength="12" show-word-limit/>
        </el-form-item>
        <el-form-item label="项目排序" prop="itemOrder">
          <el-input-number v-model="form.itemOrder" controls-position="right" :min="10" :step="10" style="width: 100%" />
        </el-form-item>
        <el-form-item label="正负标识" prop="itemSign">
          <el-select v-model="form.itemSign" placeholder="请选择正负标识" >
            <el-option
              v-for="item in ipcSignOptions.data"
              :key="item.code"
              :value="item.code"
              :label="item.name">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="计算方式" prop="computeMode">
          <el-select v-model="form.computeMode" placeholder="请选择" @change="">
            <el-option
                v-for="item in computeModeOptions.data"
                :key="item.code"
                :value="item.code"
                :label="item.name">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="百章项目" prop="hundredItem" v-if="form.computeMode == 1">
          <el-select v-model="form.hundredItem" placeholder="请选择" style="width: 100%" >
            <el-option
              v-for="item in hundredChaptersOptions.data"
              :key="item.code"
              :value="item.code"
              :label="item.name">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="求和项目" prop="sumItemArray" v-if="form.computeMode == 2">
          <el-select v-model="sumItemArray" placeholder="请选择" multiple collapse-tags style="width: 100%" @change="handleSumItemChange">
            <el-option
                v-for="item in ipcOptions"
                :key="item.itemCode"
                :value="item.itemCode"
                :label="item.itemName">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="百分比项目" prop="percentageItem" v-if="form.computeMode == 3">
          <el-select v-model="form.percentageItem" placeholder="请选择百分比项目">
            <el-option
                v-for="item in ipcOptions"
                :key="item.itemCode"
                :value="item.itemCode"
                :label="item.itemName">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="百分比系数" prop="percentageRatio" v-if="form.computeMode == 3">
          <el-input-number v-model="form.percentageRatio" controls-position="right" :min="1" :step="1" style="width: 100%" />
        </el-form-item>
        <el-form-item label="代理方法" prop="invokeTarget" v-if="form.computeMode == 5">
          <el-input v-model="form.invokeTarget" placeholder="请输入代理方法" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Ipc">
import { list, getInfo, add, update, del, selectDropDownOptionsNotSelf } from "@/api/basic/ipc";
import computeModeJson from "@/json/computeMode.json";
import hundredChaptersJson from "@/json/hundredChapters.json";
import ipcSignJson from "@/json/ipcSign.json";

const { proxy } = getCurrentInstance();
const { sys_normal_disable } = proxy.useDict("sys_normal_disable");

const ipcList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

// 中期支付证书计算方式
const computeModeOptions = ref(computeModeJson);
// 中期支付证书计算正负数
const ipcSignOptions = ref(ipcSignJson);
// 百章支付项目100-900
const hundredChaptersOptions = ref(hundredChaptersJson);
// 支付项目下拉列表数据
const ipcOptions = ref([]);
// 求和支付项目数组
const sumItemArray = ref([]);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
  },
  rules: {
    itemCode: [
      { required: true, message: "项目编号不能为空", trigger: "blur" }
    ],
    itemName: [
      { required: true, message: "项目名称不能为空", trigger: "blur" }
    ],
    itemOrder: [
      { required: true, message: "项目排序不能为空", trigger: "blur" }
    ],
    itemSign: [
      { required: true, message: "正负标识不能为空", trigger: "blur" }
    ],
    computeMode: [
      { required: true, message: "计算方式不能为空", trigger: "blur" }
    ],
    hundredItem: [
      { required: true, message: "百章项目不能为空", trigger: "blur" }
    ],
    sumItem: [
      { required: true, message: "求和项目不能为空", trigger: "blur" }
    ],
    percentageItem: [
      { required: true, message: "百分比项目不能为空", trigger: "blur" }
    ],
    percentageRatio: [
      { required: true, message: "百分比系数不能为空", trigger: "blur" }
    ]
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询基础中期支付证书列表 */
function getList() {
  loading.value = true;
  list(queryParams.value).then(response => {
    ipcList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    itemCode: null,
    itemName: null,
    itemStatus: null,
    itemSign: null,
    itemOrder: null,
    computeMode: null,
    hundredItem: null,
    sumItem: null,
    percentageItem: null,
    percentageRatio: null,
    invokeTarget: null,
    isSystem: null,
    isShow: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    remark: null
  };
  proxy.resetForm("ipcRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  selectDropDownOptionsNotSelf({itemCode: form.itemCode}).then(res => {
    ipcOptions.value = res.data;
    open.value = true;
    title.value = "添加基础中期支付证书";
  });
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getInfo(_id).then(response => {
    form.value = response.data;
    if (form.value.sumItem != '' && form.value.sumItem != null) {
      let sumItemValue = form.value.sumItem.split(",");
      sumItemValue.forEach((item) => {
        sumItemArray.value.push(item);
      });
    }
    selectDropDownOptionsNotSelf({itemCode: form.value.itemCode}).then(res => {
      ipcOptions.value = res.data;
      open.value = true;
      title.value = "修改基础中期支付证书";
    });
  });
}

// 求和项目发生改变时触发
function handleSumItemChange() {
  let sumItemValue = '';
  sumItemArray.value.forEach((item) => {
    sumItemValue += item + ',';
  })
  sumItemValue = sumItemValue.substring(0, sumItemValue.length - 1);
  form.value.sumItem = sumItemValue;
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["ipcRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        update(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        add(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除基础中期支付证书编号为"' + _ids + '"的数据项？').then(function() {
    return del(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('basic/ipc/export', {
    ...queryParams.value
  }, `ipc_${new Date().getTime()}.xlsx`)
}

getList();
</script>
